/*
This is the replication code for "Are Electoral Autocracies Good for the Poor? Evidence from Social Assistance Programs"
Final version for publication; July 20,2020


*/
 
log using "R&R_log"

/// Call to the data
use "C:\data\AutocProPoor\RepData_Revisions_June2020", clear


/// Create some necessary variables 

gen ea = 0 if v2x_regime!=.
replace ea = 1 if v2x_regime==1
tab ea, mis
tab v2x_regime, mis
label var ea "Electoral Autocracy"

gen dem = 0 if v2x_regime!=.
replace dem = 1 if v2x_regime>1
label var dem "electoral and liberal democracies"
tab dem, mis
tab v2x_regime

gen left = 0 
replace left = 1 if execrlc==3
tab execrlc, mis 
tab left, mis 
label var left "Left regime"

gen oil_new = ln(oil_gas_valuepop_2014+1)
label var oil_new "log of oil income per capita, Ross data update"

gen logpop = ln(pop_total_wbopendata)
label var logpop "Population (log)"

rename v2xps_party party_inst

gen autocsamp = 0
replace autocsamp = 1 if v2x_regime<=1

gen elec = 0
replace elec = 1 if NELDAmmdd!=.
label var elec "Exec or Leg election this year"

tab elec, mis
tab NELDAmmdd, mis

gen govage = gov1age if gov1age>=0
sum govage
gen log_govage = ln(govage)
sum log_govage
hist log_govage, freq

*label variables previously created/drawn from other sources
label var autocsamp "sample identifier for autocratic regimes"
label var party_inst "Party institutionalization, Vdem"
label var inc_comb "log income per capita, Graham et al WDI/PW combined"
label var elec "Exec or Leg election this year, NELDA"
label var WBgrow_new "Growth in income per capita, newest WDI"
label var newprog "dummy variable for any new program created this year, Dodlova et al (2017)"
label var log_govage "log age of largest party in government"


**************
**Generate Figure 1 
bysort year: gen newprog2_tot = sum(newprog2)

*with newprog2 (reports number of new programs started per year)
twoway bar newprog2_tot year if year>=1960, xlabel(1960(5)2015, ang(-45)) scheme(plotplain) bcolor(gs7) xsize(8) ///
title("Figure 1: Number of New Social Transfer Programs in Developing Countries, 1960-2015", pos(11) size(large)) ///
ytitle("Number of new programs created") xtitle(" ") ///
note("Data source: Non-contributory Social Transfer Programmes (Dodlova, Giolbas, and Lay, 2017)" ///
"Data represent 176 programs in 97 low and middle-income countries.")

**************
/// Regression analysis 

tsset ccode year

*1 Direct Effect
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)

estimates store full

* autocsamp==1
logit newprog l.ea l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
estimates store autoc

*generate supplemental material table of full results	
esttab full autoc using main.rtf, se replace 


**********************
**********************	
	
**Figure 2 - Substantive effects based on logistic regression models

**
*All regime sample
**

logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
*calculate 25-75th percentiles of interval level variables
centile party_inst inc_comb logpop total WBgrow_new oil_new al_ethnic if e(sample), centile(25 75) 

*estimate the change in pr(new program) for a given change in each explanatory variable, holding other model covariates at means
*EA
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, at(l.ea = (0 1)) atmeans level(90) contrast(at effects) post
estimates store margins1
*DEM
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, at(l.dem = (0 1)) atmeans level(90) contrast(at effects) post
estimates store margins2
*Party Institutionalization 
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, at(l.party_inst = (.4 .71)) atmeans level(90) contrast(at effects) post
estimates store margins4
*Income per capita
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, at(l.inc_comb = (6.73 8.45)) atmeans level(90) contrast(at effects) post
estimates store margins5
*Left Executive
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, at(l.left = (0 1)) atmeans level(90) contrast(at effects) post
estimates store margins6
*Population
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, at(l.logpop = (15 17)) atmeans level(90) contrast(at effects) post
estimates store margins7
*Total programs in place
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, at(l.total = (0 1)) atmeans level(90) contrast(at effects) post
estimates store margins8
*Economic growth
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, at(l.WBgrow_new = (.22 4.84)) atmeans level(90) contrast(at effects) post
estimates store margins9
*Oil income per capita
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, at(l.oil_new = (0 5.08)) atmeans level(90) contrast(at effects) post
estimates store margins10
*Ethnic Fractionalization
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, at(l.al_ethnic = (.32 .71)) atmeans level(90) contrast(at effects) post
estimates store margins11
						
**Repeat this process while limiting sample to Autocracies Only					
*EA (autocracy only sample)
logit newprog l.ea l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
margins, at(l.ea = (0 1)) atmeans level(90) contrast(at effects) post
estimates store m2_1	
*Party Institutionalization 
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
margins, at(l.party_inst = (.4 .71)) atmeans level(90) contrast(at effects) post
estimates store m2_2
*Income per capita
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
margins, at(l.inc_comb = (6.73 8.45)) atmeans level(90) contrast(at effects) post
estimates store m2_3
*Left Executive
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
margins, at(l.left = (0 1)) atmeans level(90) contrast(at effects) post
estimates store m2_4
*Population
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
margins, at(l.logpop = (15 17)) atmeans level(90) contrast(at effects) post
estimates store m2_5
*Total programs in place
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
margins, at(l.total = (0 1)) atmeans level(90) contrast(at effects) post
estimates store m2_6
*Economic growth
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
margins, at(l.WBgrow_new = (.22 4.84)) atmeans level(90) contrast(at effects) post
estimates store m2_7
*Oil income per capita
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
margins, at(l.oil_new = (0 5.08)) atmeans level(90) contrast(at effects) post
estimates store m2_8
*Ethnic Fractionalization
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
margins, at(l.al_ethnic = (.32 .71)) atmeans level(90) contrast(at effects) post
estimates store m2_9

**Plot these changes in predicted probalities as Figure 2
coefplot margins* ,  xline(0) level(95) asequation swapnames legend(off) ///
	eqrename(margins1 = "Electoral autocracy" margins2 = "Democracy" ///
	margins4 = "Party Institutionalization" margins5 = "Income per capita (log)" margins6 = "Left executive" margins7 = "Population (log)" ///
	margins8 = "Total transfer programs in place" margins9 = "Economic growth" margins10 = "Oil income per capita (log)" ///
	margins11 = "Ethno-Linguistic Fractionalization") msize(large) msymbol(circle_hollow) mcolor(gs5) mlcolor(gs5) ///
	ciopts(lcolor(gs5)) bylabel("All Regimes") ///				
	|| m2_*, level(95) asequation swapnames legend(off)  eqrename(m2_1 = "Electoral autocracy" ///
	m2_2 = "Party Institutionalization" m2_3 = "Income per capita (log)" m2_4 = "Left executive" m2_5 = "Population (log)" ///
	m2_6 = "Total transfer programs in place" m2_7 = "Economic growth" m2_8 = "Oil income per capita (log)" ///
	m2_9 = "Ethno-Linguistic Fractionalization") msize(large) msymbol(circle_hollow) mcolor(gs5) mlcolor(gs5) ///
	ciopts(lcolor(gs5)) nooffsets bylabel("Autocracies Only") aspectratio(2) byopts(row(1) legend(off) title(" " "Figure 2: Substantive Effects from Logistic Regression" "Expressed As Changes in Probability of Adopting a New Program") ///
	note(" " "Estimates expressed as the change in predicted probability of adopting a new social transfer program" ///
	"(with 95% confidence intervals) associated with a unit change in binary explanatory variables, or an" ///
	"increase from the 25th to 75th percentile values for interval-level explanatory variables. Predicted" ///
	"probabilities calculated while holding all other explanatory variables to mean values. Models include" ///
	"2,792/1,267 observations and 117/98 countries respectively. See supplemental materials for regression results.", size(small))) 
	
*calculate baseline pr(newprog) in each model	
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
margins, atmeans level(90)
logit newprog l.ea l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
margins, atmeans level(90)

**********************
**********************			
	
*Between Within Approach -  decompose IVs into their cm and dev versions
*Key variables
by ccode: egen meandv = mean(newprog)
label var meandv "Mean of DV of new program adopted"
by ccode: egen ea_cm = mean(ea)
label var ea_cm "Electoral Authoritarian, country/panel mean"
by ccode: gen ea_dev = (ea - ea_cm)
label var ea_dev "Electoral Authoritarian, deviation from country/panel mean"
by ccode: egen dem_cm = mean(dem)
label var dem_cm "Democracy, country/panel mean"
by ccode: gen dem_dev = (dem - dem_cm)
label var dem_dev "Democracy, deviation from country/panel mean"
*Control variables
by ccode: egen inc_cm = mean(inc_comb)
by ccode: gen inc_dev = (inc_comb - inc_cm)
by ccode: egen party_cm = mean(party_inst)
by ccode: gen party_dev = (party_inst - party_cm)
by ccode: egen left_cm = mean(left)
by ccode: gen left_dev = (left - left_cm)
by ccode: egen grow_cm = mean(WBgrow_new)
by ccode: gen grow_dev = (WBgrow_new - grow_cm)
by ccode: egen oil_cm = mean(oil_new)
by ccode: gen oil_dev = (oil_new - oil_cm)

*full samp / total
logit newprog l.ea_cm l.ea_dev l.dem_cm l.dem_dev l.party_cm l.party_dev l.inc_cm l.inc_dev l.left_cm l.left_dev l.logpop l.total l.grow_cm l.grow_dev l.oil_cm l.oil_dev l.al_ethnic  i.year if year>=1990, cluster(ccode)
estimates store bw1
*full samp / meandv
logit newprog l.ea_cm l.ea_dev l.dem_cm l.dem_dev l.party_cm l.party_dev l.inc_cm l.inc_dev l.left_cm l.left_dev l.logpop l.meandv l.grow_cm l.grow_dev l.oil_cm l.oil_dev l.al_ethnic  i.year if year>=1990, cluster(ccode)
estimates store bw2
*autoc samp / total
logit newprog l.ea_cm l.ea_dev l.party_cm l.party_dev l.inc_cm l.inc_dev l.left_cm l.left_dev l.logpop l.total l.grow_cm l.grow_dev l.oil_cm l.oil_dev l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
estimates store bw3
*autoc samp / meandv
logit newprog l.ea_cm l.ea_dev l.party_cm l.party_dev l.inc_cm l.inc_dev l.left_cm l.left_dev l.logpop l.meandv l.grow_cm l.grow_dev l.oil_cm l.oil_dev l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
estimates store bw4 

**	Plot these coefficients as Figure 3 in the manuscript
coefplot bw1, drop(L.party_cm L.party_dev L.inc_cm L.inc_dev L.left_cm L.left_dev L.grow_cm L.grow_dev L.oil_cm L.oil_dev L.party_inst L.inc_comb L.left L.logpop L.total L.WBgrow_new L.oil_new L.al_ethnic *.year _cons) xline(0) ///
	scheme(plotplain) level(95 90) msize(large) bylabel("All Regimes;" "Unit Effects as" "Total Programs") ///
	|| bw2, drop(L.party_cm L.party_dev L.inc_cm L.inc_dev L.left_cm L.left_dev L.grow_cm L.grow_dev L.oil_cm L.oil_dev L.party_inst L.inc_comb L.left L.logpop L.meandv L.WBgrow_new L.oil_new L.al_ethnic *.year _cons)  level(95 90) msize(large) bylabel("All Regimes;" "Unit Effects as" "Mean of DV") ///
	|| bw3, drop(L.party_cm L.party_dev L.inc_cm L.inc_dev L.left_cm L.left_dev L.grow_cm L.grow_dev L.oil_cm L.oil_dev L.dem_cm L.dem_dev L.party_inst L.inc_comb L.left L.logpop L.total L.WBgrow_new L.oil_new L.al_ethnic *.year _cons)  level(95 90) msize(large) bylabel("Autocracies Only;" "Unit Effects as" "Total Programs")  ///
	|| bw4, drop(L.party_cm L.party_dev L.inc_cm L.inc_dev L.left_cm L.left_dev L.grow_cm L.grow_dev L.oil_cm L.oil_dev L.dem_cm L.dem_dev L.party_inst L.inc_comb L.left L.logpop L.meandv L.WBgrow_new L.oil_new L.al_ethnic *.year _cons) coeflabels(L.ea_cm = "Electoral Autocracy, between effect" L.ea_dev = "Electoral Autocracy, within effect" ///
	L.dem_cm = "Democracy, between effect" L.dem_dev = "Democracy, within effect", wrap(25)) level(95 90) msize(vlarge)  bylabel("Autocracies Only;" "Unit Effects as" "Mean of DV") ///
	byopts(row(1) note(" " "Note: Models vary in terms of the range of regimes included in the sample and how unit effects are captured." "See text for full model description and covariates included but not displayed.", size(small)) ///
	title("Figure 3: Between-Within Logistic Regression Coefficients and 95% and 90% Confidence Intervals" " " , pos(11))) ciopts(lwidth(*1 *2)) aspectratio(2)
				
esttab bw1 bw2 bw3 bw4 using bw.rtf, se replace
	
**********************
**********************	
		
*Supplemental Analyses
*1 without party_inst
*2 with log_govage as alt
*3 with party_inst and elec
*all three in full, and autoc samples

*full samp
logit newprog l.ea l.dem l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
estimates store m1
logit newprog l.ea l.dem l.log_govage l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
estimates store m2
logit newprog l.ea l.dem l.party_inst l.elec l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
estimates store m3

*autocsamp
logit newprog l.ea l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
estimates store m4
logit newprog l.ea l.log_govage l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
estimates store m5
logit newprog l.ea l.party_inst l.elec l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
estimates store m6

esttab m1 m2 m3 using suppfull_se.rtf	, se replace
esttab m4 m5 m6 using suppauto_se.rtf	, se replace


**********************
**********************

*Supplemental Analysis - interactive models (with left, party_inst, govage)

*full, interact party_inst with both reg types
logit newprog l.ea l.dem l.party_inst c.l.ea#c.l.party_inst c.l.dem#c.l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
estimates store ie_p_f
margins, dydx(l.ea) cont at(l.party_inst = (.1(.1)0.9)) atmeans
marginsplot, level(95) scheme(plotplain) recastci(rarea) ciopts(fcolor(gs12) bcolor(gs12)) xtitle("Party Institutionalization") ///
	ytitle("Marginal Effect of Electoral Authoritarianism") ///
	addplot(hist party_inst if e(sample) & ea==1, bcolor(gs5) fcolor(none) percent yaxis(2) width(0.025) start(0) ylabel(0 "0%" 20 "20%" 40 "40%", axis(2)) yscale(alt lcolor() axis(2)) ///
	ytitle("Observed values of party institutionalization", axis(2)) ///
	legend(order(2 "Marginal effect and 95% CI" 3 "Observed values of" "party institutionalization in EA regimes")) ///
	yline(0) title(" ") ///
		)	
*this calculates marginal effects of democracy, but this is not reported
margins, dydx(l.dem) cont at(l.party_inst = (.1(.1)0.9)) atmeans

*autoc only, interact party_inst with ea
logit newprog l.ea l.party_inst c.l.ea#c.l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
estimates store ie_p_a
margins, dydx(l.ea) cont at(l.party_inst = (.1(.1)0.9)) atmeans
marginsplot, level(95) scheme(plotplain) recastci(rarea) ciopts(fcolor(gs12) bcolor(gs12)) xtitle("Party Institutionalization") ///
	ytitle("Marginal Effect of Electoral Authoritarianism") ///
	addplot(hist party_inst if e(sample) & ea==1, bcolor(gs5) fcolor(none) percent yaxis(2) width(0.025) start(0) ylabel(0 "0%" 20 "20%" 40 "40%" , axis(2)) yscale(alt lcolor() axis(2)) ///
	ytitle("Observed values of party institutionalization", axis(2)) ///
	legend(order(2 "Marginal effect and 95% CI" 3 "Observed values of" "party institutionalization in EA regimes")) ///
	yline(0) ///
	title(" ") ///
	)	

*full, interact left with both reg types
logit newprog l.ea l.dem l.party_inst c.l.ea#c.l.left c.l.dem#c.l.left l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
estimates store ie_l_f
margins, dydx(l.ea) cont at(l.left = (0 1)) atmeans
marginsplot, level(95) recastci(rspike) xtitle(" ") scheme(plotplain) ///
ytitle("Marginal Effect of Electoral Authoritarian") ///
title(" ") xlabel(0 "Non-left Executive" 1 "Left Executive") xsize(4) yline(0) aspectratio(1) ///
note("Marginal effect of electoral autocracy (95% ci's) in left and" ///
	"non-left executives. Calculated from model using all regime types.")

*autoc only, interact left with ea
logit newprog l.ea l.party_inst c.l.ea#c.l.left l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990 & autocsamp==1, cluster(ccode)
estimates store ie_l_a
margins, dydx(l.ea) cont at(l.left = (0 1)) atmeans
marginsplot, level(95) recastci(rspike) xtitle(" ") scheme(plotplain) ///
ytitle("Marginal Effect of Electoral Authoritarian") ///
title(" ") xlabel(0 "Non-left Executive" 1 "Left Executive") xsize(4) yline(0) aspectratio(1) ///
note("Marginal effect of electoral autocracy (95% ci's) in left and" ///
	"non-left executives. Calculated from model using autocracies only.")

esttab ie_p_f ie_p_a ie_l_f ie_l_a using ie.rtf, se replace

**********************
**********************

*Supp - list of countries included in base model 
*main model to generate e(sample)	
logit newprog l.ea l.dem l.party_inst l.inc_comb l.left l.logpop l.total l.WBgrow_new l.oil_new l.al_ethnic  i.year if year>=1990, cluster(ccode)
tab country_name if e(sample)

log close
